<template>
	<view class="points-mall">
		<view class="card product">
			<view class="product-list">
				<uni-row :gutter="30">
					<uni-col :span="12" v-for="item in dataList" :key="item.id">
						<navigator :url="`productDetails?content=${encodeURIComponent(JSON.stringify(item))}`">
							<view class="product-item">
								<image :src="item.imageKey" mode="aspectFill" :draggable="false" />
								<view class="product-text">
									<view class="product-title">{{item.name}}</view>
									<view class="product-integral"><text>{{item.price}}</text>元</view>
								</view>
							</view>
						</navigator>
					</uni-col>
				</uni-row>
			</view>
			<uni-load-more :status="loadmore" />
		</view>
	</view>
</template>

<script>
	import {productList} from '@/api/convenientServices'
	export default {
		data() {
			return {
				loadmore: 'loading',
				ifBottomRefresh: false,
				dataList: [], //数据
				total: null, //总条数
				page: {
					current: 1, //第几页
					size: 5, //显示多少条
				}
			};
		},
		methods: {
			getList(userId) {
				this.loadmore = 'loading'
				productList({...this.page, userId}).then(res => {
					//请求接口成功之后，判断加载状态，处理数据	
					if (this.ifBottomRefresh) {
						this.dataList= this.dataList.concat(res.data.records)
					} else {
						this.dataList= res.data.records
					}
					this.ifBottomRefresh = false
					this.loadmore = this.dataList.length < res.data.total ? 'more' : 'noMore'
			
				})
			},
			refresh() {
				this.page.current = 1
				this.ifBottomRefresh = false
				this.getList(this.userId)
			}
		},
		onLoad(e) {
			this.userId = e.userId
			this.getList(e.userId)
		},
		onPullDownRefresh() {
			this.refresh()
		},
		onReachBottom() {	
			this.page.current += 1
			// 没有更多数据之后 就不会在触底加载
			if (this.loadmore == 'noMore') return
			this.ifBottomRefresh = true
			this.getList(this.userId) // 触底 拉取数据
		}
	}
</script>

<style lang="scss" scoped>
	.product {
		box-shadow: 0rpx 0rpx 14rpx 0rpx rgba(0,0,0,0.08);
		border-radius: 16rpx 16rpx 0rpx 0rpx;
		.product-list {
			padding: 0 30rpx 30rpx;
			.product-item {
				margin-top: 30rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				border: 1rpx solid #E3E3E3;
				image {
					width: 300rpx;
					height: 225rpx;
					border-radius: 16rpx 16rpx 0rpx 0rpx;
				}
				.product-text {
					padding: 20rpx;
					.product-title {
						font-size: 26rpx;
						line-height: 36rpx;
						margin-bottom: 20rpx;
					}
					.product-integral {
						font-size: 20rpx;
						color: #FC0204;
						text {
							font-size: 26rpx;
							font-weight: bold;
						}
					}
				}
			}
		}
	}
</style>
